package net.jdm.constraint.instability;

import net.jdm.common.Alert;
import net.jdm.common.Alert.AlertLevel;
import net.jdm.common.Alert.DetailLevel;
import net.jdm.constraint.Constraint;
import net.jdm.domain.dependence.ContainerDependence;
import net.jdm.domain.entity.Scope;

public class InstabilityDependConstraint  implements Constraint{
	   private InstabilityPrinter instabilityPrinter=InstabilityPrinter.getInstance();
	   private double thresholdValue=0;
	   private Alert alert=new Alert();
		public InstabilityDependConstraint(double thresholdValue){
		       this.thresholdValue=thresholdValue;
		}
		public boolean check(Scope scope) {
			boolean result=true;
			for(ContainerDependence dependence:scope.getDependencesWithinMe()){
				if(dependence.getTo().isStable())
					continue;
				if(dependence.getTo().instability()-dependence.getFrom().instability()>thresholdValue){
					instabilityPrinter.printInstabilityDependMessage(this.alert,dependence,thresholdValue);
					result=false;
				}
			}
			return result;
		}
		public void setThresholdValue(double thresholdValue) {
			this.thresholdValue = thresholdValue;
		}
		public double getThresholdValue() {
			return thresholdValue;
		}
		@Override
		public void setAlertLevel(AlertLevel alertLevel) {
			alert.setAlertLevel(alertLevel);
		}
		@Override
		public void setDetailLevel(DetailLevel detailLevel) {
			this.alert.setDetailLevel(detailLevel);
		}

		public Alert getAlert() {
		    return alert;
	    }

		@Override
		public void setAlertMessage(String alertMessage) {
			this.alert.setAlertMessage(alertMessage);
		}
}
